<template>
  <div>
    <!-- 添加按钮 -->
    <el-button @click="willAdd" type="primary">添加</el-button>
    <!-- 表格 -->

    <list-vue @willUpdate="willUpdate" @init="getlist" :arr="arr"></list-vue>
    <el-pagination
      @current-change="changePage"
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="size"
    ></el-pagination>
    <!-- 弹框 -->
    <form-vue :info="info" :arr="arr" @init="getlist" ref="form"></form-vue>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import listVue from "./components/list.vue";
import formVue from "./components/form.vue";
import { requserlist, requsercount } from "../../http/api";
import { toggle } from "../../mixins";
export default {
  components: {
    listVue,
    formVue
  },
  mixins: [toggle],
  data() {
    return {
      arr: [],
      total: 0,
      size: 2,
      page: 1
    };
  },
  computed: {
    ...mapGetters({})
  },
  mounted() {
    this.getlist();
  },
  methods: {
    ...mapActions({}),
    requestList() {
      requserlist({
        page: this.page,
        size: this.size
      }).then(res => {
        if (res.data.code === 200) {
          let arr = res.data.list ? res.data.list : [];
          if (this.page != 1 && arr.length === 0) {
            this.page--;
            this.requestList();
            return;
          }
          this.arr = arr;
        }
      });
    },
    getlist() {
      this.requestList();
      requsercount().then(res => {
        if (res.data.code === 200) {
          this.total = res.data.list[0].total;
        }
      });
    },
    changePage(e) {
      this.page = e;
      this.requestList();
    }
  }
};
</script>
<style scoped lang='less'>
@import "../../less/index.less";
.el-pagination {
  float: right;
}
</style>